﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.Planner.CApAPPSegmentDepartApproachCommand
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Diagnostics;


namespace DnProcessSimulateCommands.AutomaticPathPlanner.Planner;

public class CApAPPSegmentDepartApproachCommand
{
  public CApAPPSegmentDepartApproachCommand(
    CApAPPTask task,
    int wpIndex,
    int subTaskIndex,
    int nbTaskToPlan,
    double openingGunValue,
    double perpendicularAdjustment)
  {
    this.IsStarted = false;
    this.IsFinished = false;
    this.WasCancelled = false;
    this.WasSuccessful = false;
    this.WasTimeOut = false;
    this.OpeningGunValue = openingGunValue;
    this.PerpendicularAdjustment = perpendicularAdjustment;
    this.WaypointIndex = wpIndex;
    this.SubTaskIndex = subTaskIndex;
    this.Task = task;
    this.TaskToPlan = nbTaskToPlan;
  }

  public CApAPPTask Task { get; set; }

  public int WaypointIndex { get; set; }

  public int SubTaskIndex { get; set; }

  public bool IsFinished { get; set; }

  public bool IsStarted { get; set; }

  public bool WasSuccessful { get; set; }

  public bool WasTimeOut { get; set; }

  public bool WasCancelled { get; set; }

  public void Cancel() => this.WasCancelled = true;

  private Stopwatch ElapsedTimer { get; set; }

  private double Progress { get; set; }

  private double OpeningGunValue { get; set; }

  private double PerpendicularAdjustment { get; set; }

  public void onIdle()
  {
    if (this.WasCancelled)
      this.EndPlanning(false);
    else
      this.ContinuePlanning();
  }

  public double GetProgress() => this.IsFinished ? 1.0 : this.Progress;

  public int TaskToPlan { get; set; }

  public void OnStartThread()
  {
    this.IsStarted = true;
    this.IsFinished = false;
    this.ElapsedTimer = new Stopwatch();
    this.ElapsedTimer.Start();
    while (!this.IsFinished)
    {
      this.onIdle();
      if (this.ElapsedTimer.ElapsedMilliseconds > 1000L)
      {
        this.Progress = (double) this.SubTaskIndex / (double) this.TaskToPlan;
        this.ElapsedTimer.Restart();
      }
    }
  }

  public virtual bool StartPlanning()
  {
    bool flag1 = true;
    this.IsStarted = true;
    this.IsFinished = false;
    bool flag2 = this.DoStartPlanning(this.WaypointIndex, this.SubTaskIndex);
    if (!flag1 & flag2)
    {
      this.WasSuccessful = true;
      this.IsFinished = false;
    }
    if (!flag2)
    {
      this.WasSuccessful = false;
      this.IsFinished = true;
    }
    return flag2;
  }

  protected virtual bool DoStartPlanning(int i_wpIndex, int subTaskIndex)
  {
    bool flag = false;
    EApproachDepartCreationType approachDepartCreationType = EApproachDepartCreationType.NONE;
    if (this.DoStartApproachDepartCreation(i_wpIndex, subTaskIndex, out approachDepartCreationType))
      flag = KiAppSegmentApproachDepartPlanCommandEx.Start(this.Task.KiTask, i_wpIndex, subTaskIndex, this.OpeningGunValue, this.PerpendicularAdjustment, (KiAppSegmentApproachDepartPlanCommandEx.KiAppApproachDepartPlanType) approachDepartCreationType);
    return flag;
  }

  private void EndPlanning(bool i_success)
  {
    this.DoEndPlanning(this.WaypointIndex, i_success);
    this.IsStarted = false;
    this.IsFinished = true;
    this.WasSuccessful = i_success;
  }

  protected virtual void DoEndPlanning(int i_waypointIndex, bool i_success)
  {
    KiAppSegmentApproachDepartPlanCommandEx.End(this.Task.KiTask, i_waypointIndex, i_success);
  }

  private void ContinuePlanning()
  {
    bool o_shouldContinue = false;
    bool i_success = this.DoContinuePlanning(this.WaypointIndex, ref o_shouldContinue);
    if (o_shouldContinue && i_success)
      return;
    this.EndPlanning(i_success);
  }

  protected virtual bool DoContinuePlanning(int i_waypointIndex, ref bool o_shouldContinue)
  {
    return KiAppSegmentApproachDepartPlanCommandEx.Continue(this.Task.KiTask, i_waypointIndex, ref o_shouldContinue);
  }

  private bool DoStartApproachDepartCreation(
    int i_wpIndex,
    int subTaskIndex,
    out EApproachDepartCreationType approachDepartCreationType)
  {
    bool flag1 = true;
    approachDepartCreationType = EApproachDepartCreationType.APPROACH_DEPART;
    bool flag2 = false;
    if (i_wpIndex > 0)
      flag2 = !this.Task.KiTask.GetWaypoint(i_wpIndex - 1).Fixed || !(this.Task.KiTask as KiAppRoboticTaskEx).IsDepartOrApproachWaypoint(i_wpIndex, i_wpIndex - 1);
    bool flag3 = false;
    if (i_wpIndex < this.Task.KiTask.CountWaypoints() - 1)
      flag3 = !this.Task.KiTask.GetWaypoint(i_wpIndex + 1).Fixed || !(this.Task.KiTask as KiAppRoboticTaskEx).IsDepartOrApproachWaypoint(i_wpIndex, i_wpIndex + 1);
    approachDepartCreationType = !flag2 ? (!flag3 ? EApproachDepartCreationType.NONE : EApproachDepartCreationType.DEPART) : (!flag3 ? EApproachDepartCreationType.APPROACH : EApproachDepartCreationType.APPROACH_DEPART);
    return flag1;
  }

  private bool isTimeoutReached() => false;

  public enum EPlanSuccess
  {
    SUCCESS,
    SKIP,
    CLASH_ERROR,
    TIMEOUT_ERROR,
    WAIT,
    WAIT_BEFORE_START,
  }
}
